<?php
declare (strict_types = 1);
namespace app\api\controller;
use app\api\BaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\facade\Request;

class Product extends BaseController
{
	protected $middleware = [
    	
    ];
	public function search(){
		$keywords=request()->param('keywords');
		// echo $keywords;exit;
		if(!$keywords){
			return json(['code'=>404,'msg'=>'请输入关键词']);
		}
		$data=Db::name('schoolproduct')
		     ->alias('e')
			 ->join('product a','a.id=e.product_id')
			 ->join('shopcategory b','b.id=a.category_id')
			 ->join('shopsubject c','c.id=a.subject_id')
			 ->join('shoptype d','d.id=a.typeid')
			 ->field('e.*,a.image,a.name')
			 ->whereLike('a.name','%'.$keywords.'%')
			 ->where(['e.status'=>1,'e.platform_id'=>2])
			 ->order('e.create_time desc')
			 ->select()
			 ->toArray();
	    foreach($data as $k=>$v){
	    	if (!isHttpsUrlByRegex($v['image'])) {
	    	   $data[$k]['image']=imgurl($v['image']);
	    	}
	    }
		if($data){
			return json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
	}
	public function prolist(){
		// $userid=request()->param('userid');
		$platform_id=request()->param('platform_id');
		if($platform_id){
		
		$data=Db::name('schoolproduct')
		     ->alias('e')
			 ->join('product a','a.id=e.product_id')
			 ->join('shopcategory b','b.id=a.category_id')
			 ->join('shopsubject c','c.id=a.subject_id')
			 ->join('shoptype d','d.id=a.typeid')
			 ->field('e.*,a.image,a.name')
			 ->where('e.platform_id','in',[2,$platform_id])
			 ->where(['e.status'=>1])
			 ->order('e.create_time desc')
			 ->select()
			 ->toArray();
		}else{
			$data=Db::name('schoolproduct')
			     ->alias('e')
				 ->join('product a','a.id=e.product_id')
				 ->join('shopcategory b','b.id=a.category_id')
				 ->join('shopsubject c','c.id=a.subject_id')
				 ->join('shoptype d','d.id=a.typeid')
				 ->field('e.*,a.image,a.name')
				 ->where(['e.status'=>1,'e.platform_id'=>2])
				 ->order('e.create_time desc')
				 ->select()
				 ->toArray();
		}
		foreach($data as $k=>$v){
			if (!isHttpsUrlByRegex($v['image'])) {
			   $data[$k]['image']=imgurl($v['image']);
			}
		}
		if($data){
			return json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
	}
	// 平台产品
	public function index(){
		
		$result=Db::name('schoolproduct')
		->alias('e')
		->join('product a','a.id=e.product_id')
		->join('shopcategory b','b.id=a.category_id')
		->join('shopsubject c','c.id=a.subject_id')
		->join('shoptype d','d.id=a.typeid')
		->field('e.*,a.image,a.name')
		->where(['e.status'=>1,'e.home'=>1,'e.platform_id'=>2])
		->order('e.create_time desc')
		->select()
		->toArray();
		foreach($result as $k=>$v){
			if (!isHttpsUrlByRegex($v['image'])) {
			   $result[$k]['image']=imgurl($v['image']);
			}
		}
		// print_r($result);exit;
		if($result){
			return json(['code'=>200,'msg'=>'请求成功','data'=>$result]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
		
	}
	// 产品详情
	public function productinfo(){
		$id=request()->param('id');//获取产品id
		if(!$id){
			return json(['code'=>404,'msg'=>'缺少参数']);
		}
		$pro=Db::name('schoolproduct')->where(['id'=>$id])->find();
		$result=Db::name('schoolproduct')
		->alias('e')
		->join('product a','a.id=e.product_id')
		->join('shopcategory b','b.id=a.category_id')
		->join('shopsubject c','c.id=a.subject_id')
		->join('shoptype d','d.id=a.typeid')
		->field('e.*,b.title,c.title as subject,d.name as type,a.name,a.image,a.images,a.content')
		->where(['e.product_id'=>$pro['product_id'],'e.status'=>1,'e.platform_id'=>$pro['platform_id']])
		->find();
		if($result){
			if (!isHttpsUrlByRegex($result['image'])) {
			   $result['image']=imgurl($result['image']);
			}
			
			if($result['images']){
				$img=explode(',',$result['images']);
				$imgs=[];
				foreach($img as $k=>$v){
					if (!isHttpsUrlByRegex($v)) {
					    $imgs[$k]['url']=imgurl($v);
					}else{
						 $imgs[$k]['url']=$v;
					}
				  
				}
				$result['images']=$imgs;
			}
			return json(['code'=>200,'msg'=>'请求成功','data'=>$result]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
	}
	
	// 产品分类接口
	public function category(){
		$platform_id=request()->param('platform_id');//获取学校id
		if(!$platform_id){
			return json(['code'=>404,'msg'=>'缺少参数!']);
		}
		$data=Db::name('shopcategory')
		->where(['status'=>1])
		->order('weigh asc.create_time desc')
		->select()->toArray();
		foreach($data as $k=>$v){
			$data[$k]['children']=Db::name('product')
			->alias('a')
			->join('schoolproduct b','b.product_id=a.id')
			->where(['a.category_id'=>$v['id'],'b.platform_id'=>$platform_id,'b.home'=>1])
			->order('b.create_time desc')
			->select()
			->toArray();
		}
		return json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
	}
	public function schoolprolist(){
		$id=request()->param('schoolid');//获取学校id
		if(!$id){
			return json(['code'=>404,'msg'=>'缺少参数']);
		}
		$result=Db::name('schoolproduct')
		->alias('e')
		->join('product a','a.id=e.product_id')
		->join('shopcategory b','b.id=a.category_id')
		->join('shopsubject c','c.id=a.subject_id')
		->join('shoptype d','d.id=a.typeid')
		->field('e.*,a.image,a.name')
		->where(['e.status'=>1,'e.home'=>1,'e.platform_id'=>$id,'type_id'=>2])
		->order('e.create_time desc')
		->select()
		->toArray();
		foreach($result as $k=>$v){
			if (!isHttpsUrlByRegex($v['image'])) {
			   $result[$k]['image']=imgurl($v['image']);
			}
		}
		if($result){
			return json(['code'=>200,'msg'=>'请求成功','data'=>$result]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
	}
	// 具体分类产品列表
	public function prollist(){
		$id=request()->param('id');
		$platformid=request()->param('platformid')?request()->param('platformid'):2;
		// echo $platformid;exit;
		if(!$id){
			return json(['code'=>404,'msg'=>'缺少参数']);
		}
		//查询公共产品
		$result=Db::name('product')
		->alias('a')
		->join('schoolproduct b','b.product_id=a.id')
		->where(['a.category_id'=>$id,'a.status'=>1,'b.platform_id'=>$platformid])
		->order('a.create_time desc')
		->field('b.*,a.name,a.image')
		->select()
		->toArray();
		return json(['code'=>200,'msg'=>'获取成功','data'=>$result]);
		
	}
	// 获取订单产品图
	public function proimg(){
		$id=request()->param('id');
		if(!$id){
			return json(['code'=>404,'msg'=>'缺少参数']);
		}
		$pro=Db::name('schoolproduct')->where(['id'=>$id])->find();
		$result=Db::name('product')
		->where(['id'=>$pro['product_id']])
		->field('image')
		->find();
		if($result){
			if (!isHttpsUrlByRegex($result['image'])) {
			   $result['image']=imgurl($result['image']);
			}
			return json(['code'=>200,'msg'=>'请求成功','data'=>$result]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
	}
	// 获取归还详细信息
	public function returninfo(){
		$id=request()->param('id');
		if(!$id){
			return json(['code'=>404,'msg'=>'缺少参数']);
		} 
		$pro=Db::name('schoolproduct')->where(['id'=>$id])->find();
		$result=Db::name('schoolproduct')
		->alias('e')
		->join('product a','a.id=e.product_id')
		->join('shopcategory b','b.id=a.category_id')
		->join('shopsubject c','c.id=a.subject_id')
		->join('shoptype d','d.id=a.typeid')
		->field('e.*,b.title,c.title as subject,d.name as type,a.name,a.image,a.images,a.content')
		->where(['e.product_id'=>$pro['product_id'],'e.status'=>1,'e.platform_id'=>$pro['platform_id']])
		->find();
		if($result){
			if (!isHttpsUrlByRegex($result['image'])) {
			   $result['image']=imgurl($result['image']);
			}
			
			if($result['images']){
				$img=explode(',',$result['images']);
				$imgs=[];
				foreach($img as $k=>$v){
					if (!isHttpsUrlByRegex($v)) {
					    $imgs[$k]['url']=imgurl($v);
					}else{
						 $imgs[$k]['url']=$v;
					}
				  
				}
				$result['images']=$imgs;
			}
			return json(['code'=>200,'msg'=>'请求成功','data'=>$result]);
		}else{
			return json(['code'=>404,'msg'=>'数据不存在']);
		}
		
	}
}
